﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace USM_Presenter.ViewModels
{
    [Serializable()]
    public class SlideShowViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        #endregion

        private DispatcherTimer _timer = new DispatcherTimer();
        public event EventHandler SlideChanged;

        private PresentationViewModel _parent;
        [XmlIgnore]
        public PresentationViewModel Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public SlideShowViewModel()
        {
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Interval = new TimeSpan(0, 0, _slideTime);
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            if (_currentImageIndex >= this.Images.Count - 1)
            {
                _currentImageIndex = -1;
                Enabled = Repeat;
            }

            _currentImageIndex++;

            if (SlideChanged != null) 
                SlideChanged(this, new EventArgs());

            NotifyPropertyChanged("CurrentImage");
        }

        public SlideShowViewModel(PresentationViewModel parent)
            : this()
        {
            _parent = parent;
        }

        private int _slideTime = 3;
        public int SlideTime
        {
            get { return _slideTime; }
            set
            {
                if (_slideTime != value)
                {
                    _slideTime = value;
                    _timer.Interval = new TimeSpan(0, 0, _slideTime);
                    NotifyPropertyChanged("SlideTime");
                }
            }
        }

        private bool _repeat = true;
        public bool Repeat
        {
            get { return _repeat; }
            set
            {
                if (value != _repeat)
                {
                    _repeat = value;
                    NotifyPropertyChanged("Repeat");
                }
            }
        }

        private ObservableCollection<string> _images = new ObservableCollection<string>();
        public ObservableCollection<string> Images
        {
            get { return _images; }
            set
            {
                if (value != _images)
                {
                    _images = value;
                    NotifyPropertyChanged("Images");
                }
            }
        }

        private int _currentImageIndex = 0;
        [XmlIgnore]
        public string CurrentImage
        {
            get { return Images.Count > 0 ? Images[_currentImageIndex] : null; }
        }

        private bool _enabled = false;
        [XmlIgnore]
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (_enabled != value)
                {
                    _enabled = value;
                    _timer.IsEnabled = _enabled;
                    _currentImageIndex = 0;
                    NotifyPropertyChanged("Enabled");
                }
            }
        }

        internal void Start()
        {
            this.Enabled = true;
        }

        internal void Stop()
        {
            this.Enabled = false;
        }

        internal void AddSlide(string f)
        {
            this.Images.Add(f);
        }

        internal void DeleteAllSlides()
        {
            this.Images.Clear();
        }
    }
}
